import { _decorator, Component, Sprite, SpriteFrame, UITransform } from 'cc'
import { TILE_TYPE_ENUM } from '../../Enum'

const { ccclass, property } = _decorator

/**
 * Predefined variables
 * Name = TileManager
 * DateTime = Tue Jul 05 2022 16:53:17 GMT+0800 (中国标准时间)
 * Author = Zero_Y
 * FileBasename = TileManager.ts
 * FileBasenameNoExtension = TileManager
 * URL = db://assets/Scripts/Tile/TileManager.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */
export const TILE_WIDTH = 55
export const TILE_HEIGHT = 55

@ccclass('TileManager')
export class TileManager extends Component {
  type: TILE_TYPE_ENUM
  moveable: boolean //可走
  turnable: boolean //可转向
  async init(type: TILE_TYPE_ENUM, spriteFrame: SpriteFrame, i: number, j: number) {
    this.type = type
    // 墙壁
    const wallet: Array<TILE_TYPE_ENUM> = [
      TILE_TYPE_ENUM.WALL_COLUMN,
      TILE_TYPE_ENUM.WALL_ROW,
      TILE_TYPE_ENUM.WALL_LEFT_TOP,
      TILE_TYPE_ENUM.WALL_RIGHT_TOP,
      TILE_TYPE_ENUM.WALL_LEFT_BOTTOM,
      TILE_TYPE_ENUM.WALL_RIGHT_BOTTOM,
    ]
    // 悬崖
    const cliff: Array<TILE_TYPE_ENUM> = [
      TILE_TYPE_ENUM.CLIFF_CENTER,
      TILE_TYPE_ENUM.CLIFF_LEFT,
      TILE_TYPE_ENUM.CLIFF_RIGHT,
    ]
    if (wallet.indexOf(this.type) !== -1) {
      // 当前是墙壁，不可走也不可旋转
      this.moveable = false
      this.turnable = false
    } else if (cliff.indexOf(this.type) !== -1) {
      // 当前是悬崖 不可走 可转
      this.moveable = false
      this.turnable = true
    } else if (this.type === TILE_TYPE_ENUM.FLOOR) {
      // 当前是地板 可走 可转
      this.moveable = true
      this.turnable = true
    }
    const sprite = this.addComponent(Sprite)
    sprite.spriteFrame = spriteFrame
    const transform = this.getComponent(UITransform)
    transform.setContentSize(TILE_WIDTH, TILE_HEIGHT)
    this.node.setPosition(i * TILE_WIDTH, -j * TILE_HEIGHT)
  }
}
